package com.masterclient.app.modules.cart.holder;

import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.masterclient.app.R;
import com.masterclient.app.modules.cart.adapter.ShopCartAdapterListener;
import com.masterclient.app.modules.cart.beans.SellerItemBean;

public class ShopHeadHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    private RelativeLayout rlCheck;
    private CheckBox checkBox;
    private ImageView ivCanNotCheck;
    private TextView tvShopNameTag;
    private TextView tvName;
    private LinearLayout llShop;
    private View viewDivide;

    private ShopCartAdapterListener mListener;
    private SellerItemBean sellerItemBean;
    private int position;

    public ShopHeadHolder(ViewGroup itemView, ShopCartAdapterListener listener) {
        super(LayoutInflater.from(itemView.getContext()).inflate(R.layout.view_item_cart_shop_head,itemView,false));
        mListener = listener;
        rlCheck = this.itemView.findViewById(R.id.rlCheck);
        checkBox = this.itemView.findViewById(R.id.checkBox);
        ivCanNotCheck = this.itemView.findViewById(R.id.iv_cannot_check_shop);
        tvShopNameTag = this.itemView.findViewById(R.id.tv_shop_name_tag);
        tvName = this.itemView.findViewById(R.id.tv_shop_name);
        llShop = this.itemView.findViewById(R.id.ll_shop);
        viewDivide = this.itemView.findViewById(R.id.view_divide);
    }

    public void onBindViewHolder(SellerItemBean itemBean, int position){
        if (itemBean == null){
            return;
        }
        this.position = position;
        sellerItemBean = itemBean;

        if (!itemBean.isActivity){
            viewDivide.setVisibility(View.VISIBLE);
        }else {
            viewDivide.setVisibility(View.GONE);
        }

        //商品是否可勾选
        if (itemBean.isUsable){
            checkBox.setVisibility(View.VISIBLE);
            ivCanNotCheck.setVisibility(View.GONE);
        }else {
            checkBox.setVisibility(View.GONE);
            ivCanNotCheck.setVisibility(View.VISIBLE);
        }
        //商品被选中
        if (itemBean.isCheck){
            checkBox.setChecked(true);
        }else {
            checkBox.setChecked(false);
        }
        //跨进商品店铺Tag
        if ("2".equals(itemBean.sellerType)) {
            tvShopNameTag.setVisibility(View.VISIBLE);
        } else {
            tvShopNameTag.setVisibility(View.GONE);
        }
        tvName.setText(itemBean.storeName);

        rlCheck.setOnClickListener(this);
        checkBox.setOnClickListener(this);
        llShop.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.rlCheck:
            case R.id.checkBox:
                checkBox.setChecked(!sellerItemBean.isCheck);
                if (mListener != null){
                    if (checkBox.isChecked()){
                        mListener.onCheckShopAllGoods(sellerItemBean.splitSuffix,true);
                    }else {
                        mListener.onCheckShopAllGoods(sellerItemBean.splitSuffix,false);
                    }
                }

                break;
            case R.id.ll_shop:
                mListener.onGoIntoShopPage(sellerItemBean.sellerId,sellerItemBean.storeName
                        ,sellerItemBean.isBoutique, TextUtils.isEmpty(sellerItemBean.sIsMyshop)
                                ||"2".equals(sellerItemBean.sIsMyshop)?"否":"是",position,sellerItemBean.isDealer,sellerItemBean.isAgent);
                break;
        }
    }
}
